package robocup.component.tactics;

import java.util.Arrays;

import robocup.component.SConf;
import robocup.component.ControllerNumbers.PLAY_MODE;
import robocup.component.actionfactories.TurnNeckActionFactory;
import robocup.component.actions.AttentionToAction;
import robocup.component.actions.TurnNeckAction;
import robocup.component.worldobjects.Player;

public class GoalkeeperState
    extends AbstractState {

  public boolean canWatchToBall = false;
  Player predicted_self = Player.getDummyInstance();
  /**
   * CatchBall-State makes the goalie try to catch the ball, if it is in
   * catchable area!
   * 
   * @param parent
   */
  public GoalkeeperState(
      final StateEvaluation parent) {

    super(parent);
    this.state = STATES.GOALKEEPER;
  }

  /**
   * PreCondition is canCatch() and playMode PlayOn!
   */
  @Override
  public boolean preCondition() {

    if (this.getWorld().getPlayMode() == PLAY_MODE.FREE_KICK_OWN && this
        .checkPlayerHasBall()) {
      return false;
    }
    if (this.getWorld().getPlayMode() == PLAY_MODE.INDIRECT_FREE_KICK_OTHER) {
      return false;
    }
    return (this.getWorld().isGoalie() && this.checkPlayMode());
  }

  /**
   * Most important action of goale => Prob 1.
   */
  @Override
  protected double successProbability() {

    return 1;
  }

  /**
   * Most important action of goale => Benefit 1.
   */
  @Override
  protected double successBenefit() {

    if (this.getWorld().getBallRef().getAge(this.getWorld().getBodyCycle()) > 5) {
      return 0;
    }
    return this.getProphet().getGoalieActionBenefit();
  }

  @Override
  public void setSubStateString() {

    this.subStateString = this.getProphet().getGoalieSubStateString();
  }

  @Override
  public void calculateMainAction() {

    this.mainAction = this.getProphet().getBestGoalieAction();
  }

  @Override
  public void calculateTurnNeckAction() {

    predicted_self.copy(this.getWorld().getSelfRef());
    predicted_self.predictThisAfterAction(this.mainAction);
    if (this.getProphet().getInterceptionTime() == 0) {
      this.turnNeckAction = TurnNeckActionFactory.watchPoint(predicted_self,
          this.getProphet().getPlayersThatCanKick().size() == 0 ? this
              .getWorld().getBallRef().getFuturePos(1,
                  SConf.getInstance().BALL_DECAY) : this.getProphet()
              .getPlayersThatCanKick().getFirst().getPosition());
    }
    else {
      this.turnNeckAction = TurnNeckActionFactory.watchPoint(predicted_self, 
          this.getWorld().getBallRef().getFuturePos(1,
          SConf.getInstance().BALL_DECAY));
    }
    if (this.turnNeckAction != null) {
      return;
    }
    if (this.getWorld().getBallRef().getAge(this.getWorld().getBodyCycle(),
        robocup.component.worldobjects.Ball.UPDATE_MODE.SEEN_WITHOUT_SPEED) > 2) {
      this.turnNeckAction = getProphet().getTurnNeckAction(this.state, this.mainAction, this.viewModeAction, this.getWorld()
              .getBallRef());
      return;
    }

    final double[] lookTo = new double[23];
    Arrays.fill(lookTo, -1);
    lookTo[22] = 1;
    this.turnNeckAction = this.getWorld().canKick() || this.getWorld()
        .getBallRef().getAge(this.getWorld().getBodyCycle()) > 5 ? getProphet().getTurnNeckAction(STATES.GOALKEEPER, this.mainAction,
            this.viewModeAction) : getProphet().getTurnNeckAction(STATES.GOALKEEPER, this.mainAction, this.viewModeAction,
        lookTo);
  }

  @Override
  public void calculateAttentionToAction() {
    predicted_self.copy(this.getWorld().getSelfRef());
    predicted_self.predictThisAfterAction(this.mainAction);
    TurnNeckAction tna = TurnNeckActionFactory.watchPoint(predicted_self,this.getWorld().getBallRef().getPosition());
    if (tna==null){
      Player team_mate = this.getProphet().getFastestTeammateToBall();
      if (team_mate!=null) 
        this.attentionToAction = new AttentionToAction(team_mate,this.getWorld().getTeamName());
        return;
    }  
    super.calculateAttentionToAction();
  }

}

/**
 * $Log: GoalkeeperState.java,v $ Revision 1.11 2006/11/05 13:58:02 wetzker more
 * merging - CLang + MF + some states still missing
 * 
 * Revision 1.10 2006/06/15 00:00:03 gamel rewritten precondition
 * 
 * Revision 1.9 2006/06/14 23:57:48 gamel come home in freekick without ball
 * 
 * Revision 1.8 2006/06/13 15:39:00 gamel changed turn neck directly onto ball
 * 
 * Revision 1.7 2006/06/13 14:20:54 gamel use new substate handling
 * 
 * Revision 1.6 2006/06/12 18:31:27 gamel added substates fixed "not cross ball
 * player"
 * 
 * Revision 1.5 2006/06/10 15:02:10 gamel penalty for Goalie (not ready yet)
 * 
 * Revision 1.4 2006/05/28 23:48:09 wetzker added clear ball to defender
 * debugged passing, clearball, tackling, attacking, kickNRun added stamina
 * control to kickNRun and waiting to dribbling
 * 
 * Revision 1.3 2006/05/15 22:49:52 gamel makes the goalie watch the ball ALWAYS
 * 
 * Revision 1.2 2006/05/01 18:52:01 gamel removed unused import
 * 
 * Revision 1.1 2006/04/25 16:26:48 gamel new State for Goalie Revision 1.4
 * 2006/03/26 21:58:42 wetzker created geometry package and moved geometry
 * classes from worldobject package there
 * 
 * Revision 1.3 2006/03/26 20:10:00 wetzker each player now contains its known
 * states and the playmodes of each state
 * 
 * Revision 1.2 2006/03/19 19:14:39 wetzker added wm-update for tackling players -
 * players are set to tackle dead for 10 cycles if seen tackling - added
 * TackleDeadState which does no main but all other actions - removed TackleBug
 * from TackleState in calculateMain increased interceptBall max cycles moved
 * methods canKick and canCatch from WM to Player class
 * 
 * Revision 1.1 2006/03/13 07:36:28 axle moved from ROOT/src for Maven 2
 * Buildsystem and Team/Soccerscope separation
 * 
 * Revision 1.7 2006/03/07 12:46:10 endert Merge after refactoring: - New
 * State-Interface: One Method for each action type - Action Bean removed -
 * ActionFactorys for helper methods, States calculate main-Action themselves -
 * formatted code - added State-List in Monitor (Context-Menu of Player) - other
 * smaller stuff
 * 
 * Revision 1.6.2.5 2006/03/06 21:10:54 wetzker removed isViBased from actions
 * as it wasn't needed anymore
 * 
 * Revision 1.6.2.4 2006/03/06 20:42:06 wetzker removed AB improved PlayersModel
 * by better player matching
 * 
 * Revision 1.6.2.3 2006/02/28 16:58:08 endert Organized imports, outsourced
 * unused vars, removed warnings!
 * 
 * Revision 1.6.2.2 2006/02/28 16:43:11 endert Changed State-Interface:
 * -resetState() -calculateXYAction() Usage of ActionFactories (no higher level
 * actions, these should be calculated in calculateMainAction()), Set
 * getAction() to deprecated and retactoring of variable names (wm ->
 * getWorld())
 * 
 * Revision 1.6.2.1 2006/02/25 22:07:13 wetzker formated
 * 
 * Revision 1.6 2006/02/24 18:53:47 wetzker used enums for Play_modes and
 * Referee_messages - still has to be improved after creation of new
 * Play_mode-Model - tagged old version removed millions of warnings
 * 
 * Revision 1.5 2006/02/22 16:04:35 endert This is done: - Rebound SoccerScope
 * to Agents (can be added or removed in config-file) - Situations now all
 * extend abstract class Situation - Refactorings: ActionBean _ removed from
 * Attributes - SoccerScope will automatically connect to host given in
 * Start-Call - Trainer uses the same host as soccerscope - Removed unused stuff
 * (old confs, bat, etc.) - Code formatted (good luck when merging) - organized
 * imports - all that i forgot to mention here Revision 1.4 2006/02/06 20:42:52
 * endert New Say-Method in Action Bean, which is called after execAcion in
 * StateEvaluation
 * 
 * Revision 1.3 2006/02/06 18:11:15 wetzker refactored, farmatted and cleaned
 * the states Revision 1.2 2006/02/02 22:48:01 gamel added for new goalie
 * 
 * Revision 1.1.2.1 2005/12/15 23:59:25 gamel new version of goalie splitted
 * into many states and replaced many action-predictions with geometry - there
 * are many bugs inside until now, so beware
 * 
 * Revision 1.5 2005/08/16 16:26:34 wetzker merged composite branch
 * 
 * Revision 1.4.2.1 2005/07/18 08:59:55 wetzker debugged: - shootGoal, now
 * working quiet well (but still only a workaround) - attackInLine - bodyCycle()
 * was wrong :/ - dribbling added: - stamina management, use
 * p.correctDashPower(StaminaLevel) moved: - ControllerNumbers, as they aren't
 * use solo by the connection
 * 
 * Revision 1.4 2005/06/29 11:45:59 gamel moved Enum STATES from ActionBean to
 * AbstractState
 * 
 * Revision 1.3 2005/06/28 08:38:28 gamel States that use ActionBean, now give
 * themselves as an argument to ActionBean
 * 
 * Revision 1.2 2005/06/15 17:03:11 wetzker removed JIAC from Head + changes in
 * framework please report bugs or shortcomings some scenarios might not work
 * because of wrong imports
 * 
 * Revision 1.1.2.1 2005/06/09 12:31:37 endert Removal of JIAC Libraries and
 * dependencies from project, restructuring the framework and improve some
 * aspects (hoping it is an improvement)
 * 
 * Revision 1.5 2005/05/09 16:55:00 endert New State Framework: Superclass is
 * Abstract State, which is extended by SimpleState and CompositeState. All
 * Other States extend one of them. PlayMode and Conditions are (up to now) not
 * inherited, but can be set in Constructor for some states (PlayModes).
 * Evaluation of States considers postCondition as well.
 * 
 * Revision 1.4 2005/05/09 09:14:04 karlb Added CVS log tag
 * 
 */
